package DBHelper;

import exceptions.ObjectNotFoundException;
import java.math.BigInteger;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBObject {
    private String name;
    private BigInteger objectId;
    private BigInteger parentId;
    private BigInteger objectTypeId;
    private String description;
    
    public DBObject() { }
    
    public DBObject(String _name, BigInteger _parentId, BigInteger _objectTypeId, String _description) {
        name = _name;
        parentId = _parentId;
        objectTypeId = _objectTypeId;
        description = _description;
    }
    
    public DBObject(BigInteger id) throws SQLException, ObjectNotFoundException {
        String query = "select * from objects where object_id = "+id;
        ResultSet rs = DBHelper.executeQuery(query);
        boolean flag = true;
        while (rs.next()) {
            try {
                flag = false;
                name = rs.getString("name");
                objectId = new BigInteger(rs.getString("object_id"));
                parentId = new BigInteger(rs.getString("parent_id"));
                description = rs.getString("description");
            }    
            catch(Exception e) {}
        }
        rs.close();
//        else {throw new ObjectNotFoundException("Can't find object with id = "+id);}
        if (flag) {
            throw new ObjectNotFoundException("Can't find object with id = "+id);
        }
    }
    
    public String getName() {
        return name;
    }
    public BigInteger getId() {
        return objectId;
    }
    public BigInteger getParentId() {
        return parentId;
    }
    public BigInteger getObjectTypeId() {
        return objectTypeId;
    }
    public String getDescription() {
        return description;
    }
    public void setName(String name) throws SQLException {
        this.name = name;
    }
    public void setParentId(BigInteger parentId) throws SQLException {
        this.parentId = parentId;
    }
    public void setObjectTypeId(BigInteger objectTypeId) throws SQLException {
        this.objectTypeId = objectTypeId;
    }
    public void setDescription(String descr) throws SQLException {
        this.description = descr;
    }
    public Param getParam(BigInteger attrId) throws SQLException {
        return new Param(objectId, attrId);
    }
}
